package com.example.administrator.chinafood;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.chinafood.Adapter.ForumListView;
import com.example.administrator.chinafood.Adapter.HomepageListView;
import com.example.administrator.chinafood.Adapter.MyListViewAdapter;
import com.example.administrator.chinafood.Adapter.MyViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.TooManyListenersException;

public class FlHomepage extends Fragment {

    private ViewPager mViewPager;//声明ViewPager
    private List<View> viewLists;
    private MyViewPagerAdapter mAdapter;//ViewPager适配器
    private GridView gridView;
    private ListView listview;
    private MyListViewAdapter<HomepageListView> grid_Adapter = null;
    private MyListViewAdapter<ForumListView> list_Adapter = null;
    private List<HomepageListView> mData1 = null;
    private List<ForumListView> mData2 = null;

    private static final int TIME = 5*1000;
    private Handler mHandler = new Handler();
    private int itemPosition = 0;
    private Context context;
    private String str[] = {"醋溜白菜","开心土豆泥","红枣当归粥","蚝油生菜"};
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fl_homepage,container,false);
        mViewPager= (ViewPager) view.findViewById(R.id.vp_homepage);
        int []img = {R.drawable.img_5,R.drawable.img_5,R.drawable.img_5,R.drawable.img_5,R.drawable.img_5};
        String []title = {"食物一","食物二","食物三","食物四","食物五"};
        final String []content = {"烤鸭是具有世界声誉的北京著名菜式，起源于中国南北朝时期，"
                ,"《食珍录》中已记有炙鸭，在当时是宫廷食品。用料为优质肉食鸭北京鸭"
                , "，果木炭火烤制，色泽红润，肉质肥而不腻，外脆里嫩。"
                , "北京烤鸭分为两大流派，而北京最著名的烤鸭店"
                ,"这里6666666666"};
        viewOnCreat(img,title,content,inflater);
        mAdapter=new MyViewPagerAdapter(5,viewLists);
        mViewPager.setAdapter(mAdapter);
        mHandler.postDelayed(runnableForViewPager, TIME);

        gridView = (GridView)view.findViewById(R.id.homepage_gridView);
        listview = (ListView)view.findViewById(R.id.homepage_listView);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ForumListView f = mData2.get(position);
                String [] string = {f.getTitle(),f.getAuthor(),f.getTime(),f.getContent(),f.getNum()};
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putStringArray("string",string);
                bundle.putInt("img",f.getImg());
                bundle.putInt("personid",f.getPersonid());
                intent.putExtras(bundle);
                intent.setClass(context,TheForum.class);
                startActivity(intent);
            }
        });

        context = view.getContext();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("foodname",str[position % 4]);
                intent.setClass(context, TheFood.class);
                startActivity(intent);
            }
        });
        init();

        return view;
    }

    public void viewOnCreat (int[] img, String[] title, String[] content, LayoutInflater inflater){
        viewLists = new ArrayList<>();
        for (int i = 0; i < 5; i++){
            View view = inflater.inflate(R.layout.adapter_viewpager,null);
            ImageView imageView = (ImageView)view.findViewById(R.id.viewpager_img);
            imageView.setBackgroundResource(img[i]);
            viewLists.add(view);
        }
    }

    Runnable runnableForViewPager = new Runnable() {
        @Override
        public void run() {
            try {
                itemPosition++;
                mHandler.postDelayed(this, TIME);
                mViewPager.setCurrentItem(itemPosition % 5);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private void init() {
        mData1 = new ArrayList<HomepageListView>();

        mData1.add(new HomepageListView(R.drawable.img_0));
        mData1.add(new HomepageListView(R.drawable.img_1));
        mData1.add(new HomepageListView(R.drawable.img_3));
        mData1.add(new HomepageListView(R.drawable.img_4));
        mData1.add(new HomepageListView(R.drawable.img_0));
        mData1.add(new HomepageListView(R.drawable.img_1));
        mData1.add(new HomepageListView(R.drawable.img_3));
        mData1.add(new HomepageListView(R.drawable.img_4));

        grid_Adapter = new MyListViewAdapter<HomepageListView>((ArrayList)mData1,R.layout.adapter_gridview_homepage) {
            @Override
            public void bindView(ViewHolder holder, HomepageListView obj) {
                holder.setImageResource(R.id.list_img,obj.getImg());
            }
        };
        mData2 = new ArrayList<ForumListView>();
        mData2.add(new ForumListView("论白菜的100种做法","author","2012/12/12","233","23333",R.drawable.img_0,0));
        mData2.add(new ForumListView("论黑暗料理的出发点","author","2012/12/12","233","23333",R.drawable.img_0,0));
        mData2.add(new ForumListView("为什么有黑暗料理","author","2012/12/12","233","23333",R.drawable.img_0,0));
        mData2.add(new ForumListView("美味菜如何出现","author","2012/12/12","233","23333",R.drawable.img_0,0));
        mData2.add(new ForumListView("美味大全推荐","author","2012/12/12","233","23333",R.drawable.img_0,0));
        list_Adapter = new MyListViewAdapter<ForumListView>((ArrayList)mData2,R.layout.adapter_listview_forum) {
            @Override
            public void bindView(ViewHolder holder, ForumListView obj) {
                holder.setText(R.id.forum_list_title,obj.getTitle());
                holder.setText(R.id.forum_list_author,obj.getAuthor());
                holder.setText(R.id.forum_list_time,obj.getTime());
                holder.setText(R.id.forum_list_num,obj.getNum());
            }
        };

        gridView.setAdapter(grid_Adapter);
        listview.setAdapter(list_Adapter);
    }

}
